package com.chandon.T04_stream.test;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

/**
 * a.平均成绩
 * b.提取成绩存入新的容器
 * c.student存入map中
 *
 * @author Dong
 * @version 1.0
 * @date 2021-04-20 15:39
 */

public class T02_StreamTest4 {
    public static void main(String[] args) {
        List<Student> students = Arrays.asList(
                Student.builder().name("岑小村").scope(59).build(),
                Student.builder().name("谷天洛").scope(82).build(),
                Student.builder().name("渣渣辉").scope(98).build(),
                Student.builder().name("蓝小月").scope(65).build(),
                Student.builder().name("皮几万").scope(70).build()
        );

//        a(students);
//        b(students);
        c(students);

    }

    public static void a(List<Student> studentList) {
        OptionalDouble optionalDouble = studentList.stream().mapToInt(Student::getScope).average();
        optionalDouble.ifPresent(System.out::println);

    }

    public static void b(List<Student> studentList) {
        List<Integer> collect = studentList.stream().map(Student::getScope).collect(Collectors.toList());
        System.out.println(collect.toString());
    }

    public static void c(List<Student> studentList) {
        Map<String, Integer> mapStudent = studentList.stream().collect(Collectors.toMap(Student::getName, Student::getScope));
        System.out.println(mapStudent);

    }

    @Data
    @AllArgsConstructor
    @Builder
    static class Student {
        String name;
        int scope;
    }

}
